package com.example.hm_dtfkz.ui.activity;

import android.content.Intent;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewbinding.ViewBinding;

import com.android.base.util.http.RxhttpUtil;
import com.example.hm_dtfkz.databinding.BillRun2ActivityBinding;
import com.example.hm_dtfkz.entity.BillTypeEntity;
import com.example.hm_dtfkz.entity.BillTypeEntity2;
import com.example.hm_dtfkz.ui.adapter.BillRunAdapter;
import com.example.hm_dtfkz.ui.adapter.BillRunAdapter2;
import com.example.hm_dtfkz.ui.base.VBBaseActivity;
import com.example.hm_dtfkz.util.DQLog;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AllBillActivity extends VBBaseActivity {
    private BillRun2ActivityBinding billRun2ActivityBinding;

    private List<BillTypeEntity2> billEntityList = new ArrayList<>();
    private StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
    @Override
    protected ViewBinding getViewBinding() {
        billRun2ActivityBinding=BillRun2ActivityBinding.inflate(getLayoutInflater());
        return billRun2ActivityBinding;
    }

    @Override
    public void initView() {
        super.initView();




        BillRunAdapter2 billRunAdapter=new BillRunAdapter2();
        RxhttpUtil.getInstance().get("http://service.picasso.adesk.com/v1/vertical/vertical?limit=30&skip=180&adult=false&first="+getIntent().getStringExtra("type")+"&order=hot",new HashMap<>(), this, new RxhttpUtil.RxHttpCallBack() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(String response) {
                DQLog.e("RxhttpUtil==200" + response);

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String data = jsonObject.getString("res");
                    BillTypeEntity2 qaIncomeResp = new Gson().fromJson(data, BillTypeEntity2.class);
                    billEntityList=qaIncomeResp.getVertical();
                    billRun2ActivityBinding.rrBill.setLayoutManager(staggeredGridLayoutManager);
                    billRunAdapter.setNewInstance(billEntityList);
                    billRun2ActivityBinding.rrBill.setAdapter(billRunAdapter);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }


            }

            @Override
            public void onError(String error) {
                DQLog.e("RxhttpUtil==error" + error);

            }

            @Override
            public void onFinish() {

            }
        });


        billRunAdapter.setOnItemClickLister(new BillRunAdapter2.OnItemClickLister() {
            @Override
            public void onItemClick(int position, ImageView tvAnswer) {
                Intent intent= new Intent();
                intent.putExtra("type",billEntityList.get(position).getPreview());
                intent.setClass(AllBillActivity.this, WebViewActivity.class);
                startActivity(intent);
            }
        });
    }

}
